package cn.com.xinjinfu.pdoulcProduct.adapter;

import android.app.Activity;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.entity.InvestBills;
import cn.com.xinjinfu.pdoulcProduct.utils.TextViewUtil;

/**
 * Created by android-adt on 2016/2/25.
 */
public class HuankuanAdapter extends BaseAdapter {
    private ArrayList<InvestBills> list;
    private LayoutInflater inflater;
    SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
    public void setList(ArrayList<InvestBills> list) {
        this.list = list;
    }

    public HuankuanAdapter(Activity context) {
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView==null){
            holder=new ViewHolder();
            convertView=inflater.inflate(R.layout.item_huikuanjihua, null);
            holder.tv_huankuan_state = (TextView) convertView.findViewById(R.id.tv_huankuan_state);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            holder.tv_lixi = (TextView) convertView.findViewById(R.id.tv_lixi);
            holder.tv_benjin = (TextView) convertView.findViewById(R.id.tv_benjin);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        InvestBills item = list.get(position);
        convertView.setVisibility(View.VISIBLE);
        if(item.isben()){
            holder.tv_benjin.setVisibility(View.VISIBLE);
            holder.tv_lixi.setVisibility(View.GONE);
            holder.tv_money.setText(TextViewUtil.getSumLength(item.getReceive_corpus()));
        }else{
            holder.tv_benjin.setVisibility(View.GONE);
            holder.tv_lixi.setVisibility(View.VISIBLE);
            holder.tv_money.setText(TextViewUtil.getSumLength(item.getReceive_interest()));
        }
        holder.tv_time.setText(format.format(new Date(item.getReceive_time())));
        int color = 0xFF6ED3D6;
        String zhuangtai="";
        switch (item.getStatus()) {
            case 0:
                zhuangtai="正常还款";
                color=0xFF6ED3D6;
                break;
            case 1:
                zhuangtai="提前还款";
                color=0xFFF77F96;
                if(item.isben()){
                    holder.tv_benjin.setVisibility(View.VISIBLE);
                    holder.tv_lixi.setVisibility(View.GONE);
                    holder.tv_money.setText(TextViewUtil.getSumLength(item.getReal_receive_corpus()));
                }else{
                    holder.tv_benjin.setVisibility(View.GONE);
                    holder.tv_lixi.setVisibility(View.VISIBLE);
                    holder.tv_money.setText(TextViewUtil.getSumLength(item.getReal_receive_interest()));
                }
                holder.tv_time.setText(format.format(new Date(item.getReal_receive_time())));
                break;
            case -7:
                zhuangtai="已转出";
                color=0xFFcccccc;
                break;
            default:
                zhuangtai="未收款";
                color=0xFFcccccc;
                break;
        }
        holder.tv_huankuan_state.setText(zhuangtai);
        GradientDrawable myGrad = (GradientDrawable)holder.tv_huankuan_state.getBackground();
        myGrad.setColor(color);
        return convertView;
    }

    class ViewHolder{
        TextView tv_huankuan_state;
        TextView tv_time;
        TextView tv_money;
        TextView tv_lixi;
        TextView tv_benjin;
    }
}
